package gw.com.android.ui.chart.views;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gwtsz.gts2.hx.R;

import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppContances;
import gw.com.android.app.AppMain;
import gw.com.android.ui.trade.TradeBusiness;
import gw.com.android.utils.ColorThemeUtil;
import gw.com.jni.library.terminal.GTSConst;
import www.com.library.model.DataItemDetail;
import www.com.library.model.DataItemResult;
import www.com.library.util.NetworkMonitor;
import www.com.library.view.UISpannableStringBuilder;

/**
 * Created by hubble on 2017/3/8.
 */

public class PositionInQuoteAdapter extends BaseAdapter {
    private Activity mContext;
    private DataItemResult mList = new DataItemResult();
    private ListView mLvTarget;
    private boolean isNight = false;
    private int mExpandOrderId = -1;
    /**
     * 处理字符串的颜色值
     */
    protected UISpannableStringBuilder m_spannableTitle;

    public PositionInQuoteAdapter(Activity context, ListView target) {
        this.mContext = context;
        this.mLvTarget = target;
        m_spannableTitle = new UISpannableStringBuilder();
    }

    public void updateData(DataItemResult data) {
        this.mList = data;
        notifyDataSetChanged();
    }

    public void onNightMode(boolean isNight) {
        this.isNight = isNight;
    }

    @Override
    public int getCount() {
        return mList.getDataCount();
    }

    @Override
    public Object getItem(int position) {
        return mList.getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mContext.getLayoutInflater().inflate(R.layout.list_item_position_in_quote, null);
            holder = new ViewHolder();
            holder.mTvOrderDir = (TextView) convertView.findViewById(R.id.tv_order_dir);
            holder.mTvLots = (TextView) convertView.findViewById(R.id.tv_lots);
            holder.mTvPrice = (TextView) convertView.findViewById(R.id.tv_price);
            holder.mTvLimitLoss = (TextView) convertView.findViewById(R.id.tv_limit_loss);
            holder.mTvLimitWin = (TextView) convertView.findViewById(R.id.tv_limit_win);
            holder.mTvProfit = (TextView) convertView.findViewById(R.id.tv_profit);
            holder.mTvLimitLW = (TextView) convertView.findViewById(R.id.tv_limit_wl);
            holder.mTvClosePosition = (TextView) convertView.findViewById(R.id.tv_close_position);
            holder.mLlCollapsed = (LinearLayout) convertView.findViewById(R.id.ll_collapsed);
            holder.mIvSwitch = (ImageView) convertView.findViewById(R.id.iv_expand);
            holder.divider4 = convertView.findViewById(R.id.divider4);
            holder.dividerBottom = convertView.findViewById(R.id.divide_bottom);
            holder.mRlExpanded = (RelativeLayout) convertView.findViewById(R.id.rl_expanded);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (isNight) {
            holder.mTvLots.setTextColor(mContext.getResources().getColor(R.color.color_f));
            holder.mTvPrice.setTextColor(mContext.getResources().getColor(R.color.color_f));
            holder.mTvLimitLoss.setTextColor(mContext.getResources().getColor(R.color.color_f));
            holder.mTvLimitWin.setTextColor(mContext.getResources().getColor(R.color.color_f));
            holder.mIvSwitch.setColorFilter(mContext.getResources().getColor(R.color.color_night_text));
            holder.mLlCollapsed.setBackgroundColor(mContext.getResources().getColor(R.color.color_night_bg_dark));
            holder.mTvLimitLW.setTextColor(mContext.getResources().getColor(R.color.color_night_text));
            holder.mTvClosePosition.setTextColor(mContext.getResources().getColor(R.color.color_night_text));
            holder.divider4.setBackgroundColor(mContext.getResources().getColor(R.color.color_night_line));
            holder.dividerBottom.setBackgroundColor(mContext.getResources().getColor(R.color.color_night_line));
        } else {
            holder.mTvLots.setTextColor(mContext.getResources().getColor(R.color.color_c));
            holder.mTvPrice.setTextColor(mContext.getResources().getColor(R.color.color_c));
            holder.mTvLimitLoss.setTextColor(mContext.getResources().getColor(R.color.color_c));
            holder.mTvLimitWin.setTextColor(mContext.getResources().getColor(R.color.color_c));
            holder.mIvSwitch.setColorFilter(null);
            holder.mLlCollapsed.setBackgroundColor(mContext.getResources().getColor(R.color.color_chart_k));
            holder.mTvLimitLW.setTextColor(mContext.getResources().getColor(R.color.color_c));
            holder.mTvClosePosition.setTextColor(mContext.getResources().getColor(R.color.color_c));
            holder.divider4.setBackgroundColor(mContext.getResources().getColor(R.color.color_d));
            holder.dividerBottom.setBackgroundColor(mContext.getResources().getColor(R.color.color_d));
        }

        final DataItemDetail mItem = mList.getItem(position);

        // 处理 收缩
        if (mExpandOrderId == mItem.getInt(GTSConst.JSON_KEY_ID)) {
            holder.mLlCollapsed.setVisibility(View.VISIBLE);
            holder.mIvSwitch.setImageResource(R.mipmap.a_small_developup);
        } else {
            holder.mLlCollapsed.setVisibility(View.GONE);
            holder.mIvSwitch.setImageResource(R.mipmap.a_small_developdown);
        }

        holder.mRlExpanded.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mExpandOrderId == mItem.getInt(GTSConst.JSON_KEY_ID)) {
                    mExpandOrderId = -1;
                } else {
                    mExpandOrderId = mItem.getInt(GTSConst.JSON_KEY_ID);
                }
                notifyDataSetChanged();


                if (position < getCount() - 1) {
                    mLvTarget.smoothScrollToPosition(position + 1);
                }

            }
        });

        holder.mTvClosePosition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!checkTradable(mItem)) {
                    return;
                }

                ActivityManager.showCloseOrderActivity(mContext, mItem, AppContances.TRADE_TYPE_FORM_CHART);
            }
        });

        holder.mTvLimitLW.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!checkTradable(mItem)) {
                    return;
                }

                ActivityManager.showOrderModifyActivity(mContext, mItem, AppContances.TRADE_TYPE_FORM_CHART);
            }
        });

        // 显示数据
        // 买入 或卖出
        if (mItem.getInt(GTSConst.JSON_KEY_DIRECTION) == AppContances.TRADE_TYPE_BUY) {
            holder.mTvOrderDir.setText(R.string.quote_title_buy);
            holder.mTvOrderDir.setTextColor(ColorThemeUtil.instance().color_trade_buy);
        } else {
            holder.mTvOrderDir.setText(R.string.quote_title_sell);
            holder.mTvOrderDir.setTextColor(ColorThemeUtil.instance().color_trade_sell);
        }
        // 手数
        holder.mTvLots.setText(AppMain.getAppString(R.string.order_trade_lot, mItem.getString(GTSConst.JSON_KEY_VOLUME)));
        // 价格
        holder.mTvPrice.setText(mItem.getString(GTSConst.JSON_KEY_OPENPRICE));
        if (mItem.getString(GTSConst.JSON_KEY_STOPLOSS) != null && mItem.getString(GTSConst.JSON_KEY_STOPLOSS).length() > 0 && !mItem.getString(GTSConst.JSON_KEY_STOPLOSS).equals("0")) {
            m_spannableTitle.reset();
            m_spannableTitle.setForegroundColorSpanString(AppMain.getAppString(R.string.order_item_title_loss), ColorThemeUtil.instance().color_c);
            if (mItem.getInt(GTSConst.JSON_KEY_STOPLOSSSTATE) != 0) {
                m_spannableTitle.setBackgroundColorSpanString(
                        mItem.getString(GTSConst.JSON_KEY_STOPLOSS), ColorThemeUtil.instance().color_t);
            } else {
                m_spannableTitle.setBackgroundColorSpanString(
                        mItem.getString(GTSConst.JSON_KEY_STOPLOSS), ColorThemeUtil.instance().transparent);
            }
            holder.mTvLimitLoss.setText(m_spannableTitle.getBuilder());
            holder.mTvLimitLoss.setVisibility(View.VISIBLE);
        } else {
            holder.mTvLimitLoss.setText(AppMain.getAppString(R.string.order_item_title_loss) + 0);
            holder.mTvLimitLoss.setVisibility(View.VISIBLE);
        }
        if (mItem.getString(GTSConst.JSON_KEY_STOPPROFIT) != null && mItem.getString(GTSConst.JSON_KEY_STOPPROFIT).length() > 0 && !mItem.getString(GTSConst.JSON_KEY_STOPPROFIT).equals("0")) {
            m_spannableTitle.reset();
            m_spannableTitle.setForegroundColorSpanString(AppMain.getAppString(R.string.order_item_title_profit), ColorThemeUtil.instance().color_c);
            if (mItem.getInt(GTSConst.JSON_KEY_STOPPROFITSTATE) != 0) {
                m_spannableTitle.setBackgroundColorSpanString(
                        mItem.getString(GTSConst.JSON_KEY_STOPPROFIT), ColorThemeUtil.instance().color_t);
            } else {
                m_spannableTitle.setBackgroundColorSpanString(
                        mItem.getString(GTSConst.JSON_KEY_STOPPROFIT), ColorThemeUtil.instance().transparent);
            }
            holder.mTvLimitWin.setText(m_spannableTitle.getBuilder());
            holder.mTvLimitWin.setVisibility(View.VISIBLE);
        } else {
            holder.mTvLimitWin.setText(AppMain.getAppString(R.string.order_item_title_profit) + 0);
            holder.mTvLimitWin.setVisibility(View.VISIBLE);
        }
        // 盈利
        holder.mTvProfit.setText(mItem.getString(GTSConst.JSON_KEY_PROFIT));
        ColorThemeUtil.instance().setPriceColor(holder.mTvProfit, mItem.getInt(GTSConst.JSON_KEY_PROFITSTATE));
        return convertView;
    }


    /**
     * 下单判断
     *
     * @return
     */
    private boolean checkTradable(DataItemDetail mModel) {

        if (!NetworkMonitor.hasNetWork()) {
            return false;
        }

        if (!TradeBusiness.PreOrderCheck(mContext, mModel.getInt(GTSConst.JSON_KEY_ZONE), mModel.getInt(GTSConst.JSON_KEY_TRADESTATE), false)) {
            return false;
        }

        return true;
    }

    class ViewHolder {
        TextView mTvOrderDir;
        TextView mTvLots;
        TextView mTvProfit;
        TextView mTvPrice;
        TextView mTvLimitLoss;
        TextView mTvLimitWin;
        TextView mTvLimitLW;
        TextView mTvClosePosition;
        LinearLayout mLlCollapsed;
        RelativeLayout mRlExpanded;
        View divider4;
        View dividerBottom;
        ImageView mIvSwitch;
    }
}
